package srst.ai.kmeansUtil;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.math3.ml.clustering.CentroidCluster;
import org.apache.commons.math3.ml.clustering.KMeansPlusPlusClusterer;

import srst.ai.CosinSimilarityUtil;
import srst.util.FilePathUtil;


/** 
* @author  作者 : YUHU YUAN
* @date 创建时间：2017年5月24日 上午11:44:24 
* @version 1.0  
*/

public class KMeansUtil {
	
	/** 
	* @author  作者 : YUHU YUAN
	* @date 创建时间：2017年5月24日 下午3:34:39 
	* @parameter resume_vectorMap
	* @return
	* @throws
	*/
	public static void computeKmeans(HashMap<String, String> resume_vectormap){
		List<LocationWrapper> clusterInput = new ArrayList<LocationWrapper>(resume_vectormap.size());
		for(String resumeName :  resume_vectormap.keySet()){
			Double[] vectorD = handleHerbVec(resume_vectormap.get(resumeName));
			double[] vectord = new double[vectorD.length];
			for(int i=0; i<vectorD.length; ++i ){
				vectord[i] = vectorD[i];
			}
			clusterInput.add(new LocationWrapper(resumeName, vectord));
		}
		
		KMeansPlusPlusClusterer<LocationWrapper> clusterer = new KMeansPlusPlusClusterer<LocationWrapper>(5, 10000);
		
		List<CentroidCluster<LocationWrapper>> clusterResults = clusterer.cluster(clusterInput);
		
		for (int i=0; i<clusterResults.size(); i++) {
		    System.out.println("Cluster " + i);
		    for (LocationWrapper locationWrapper : clusterResults.get(i).getPoints())
		        System.out.println(locationWrapper.getLocation());
		    System.out.println();
		}
		
		
	}
	
	
	
	private static Double[] handleHerbVec(String string){
		String[] values = string.split(" ");
		List<Double>   valueList= new ArrayList<>();  
		for(String temp : values){
			temp = temp.trim();
			if(!temp.equals("")){
				valueList.add(Double.valueOf(temp));
			}
		}
		Double[]  vector = new Double[valueList.size()];
		vector= valueList.toArray(vector);
		return vector;
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		try {
			CosinSimilarityUtil.computeCosSim2Text(FilePathUtil.RESUME_VECTOR_PATH, FilePathUtil.COSINSIMILARITY_PATH);
			KMeansUtil.computeKmeans(CosinSimilarityUtil.getHerbmap());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("运行结束");
	}

}
















